<template>
  <router-link class="g-col-2 clearfix" :to="{path: (link || '/learn/detail'), query: {id: item.id}}" :style="'width: ' + width + ';'">
    <img ref="bigImgRef" :src="item.image" @load="resetHeight"/>
    <div class="g-pack-bg"></div>
    <!--            <span class="g-pack-rt">-->
    <!--              <span class="g-pack-tag g-pack-tag-red">热门</span>-->
    <!--            </span>-->
    <!--            <span class="g-pack-rb">-->
    <!--              <span>更新至第8章</span>-->
    <!--            </span>-->
    <p class="g-pack-title">
      {{item.name}}
    </p>
    <div class="bottom clearfix">
      <span class="price">{{formatPrice(item.price) || "免费"}}</span>
      <span class="learn">{{item.learnNum || 0}}人报名</span>
    </div>
  </router-link>
</template>

<script>
  import { ref, nextTick} from "vue";
  export default {
    name: "bigRectangle",
    props: {
      item: {
        type: Object
      },
      width: {
        type: String,
        default: "400px"
      },
      link: {
        type: String,
        default: ""
      }
    },
    setup () {
      const bigImgRef = ref({height: 0})
      const imgHeight = ref("auto")
      const resetHeight = function() {
        if (bigImgRef.value.height !== 0) {
        console.log("reset", bigImgRef.value.height)
        imgHeight.value = bigImgRef.value.offsetHeight + 103 + "px"
        }
      }
      nextTick(function () {
        window.addEventListener("resize", () => {
          imgHeight.value = "auto"
        });
      })
      const formatPrice = (p) => {
        if (!p) {
          p = 0
        }
        if (p === 0) {
          return "免费"
        }
        return p.toFixed(2);
      }
      return {
        bigImgRef,
        imgHeight,
        resetHeight,
        formatPrice
      }
    }
  }
</script>

<style scoped lang="scss">
.g-col-2 {
  float: left;
  box-sizing: border-box;
  vertical-align: top;
  margin: 10px;
  background: #FFFFFF;
  border-radius: 4px;
  box-shadow: 0 1px 2px rgb(100 100 100 / 10%);
  &:hover {
    transform: translateY(-2px);
  }
  img {
    display: block;
    width: 100%;
    margin-bottom: 8px;
    border-radius: 4px 4px 0 0;
    height: 418px;
  }
  .g-pack-title {
    word-break: break-all;
    color: #545C63;
    line-height: 20px;
    height: 40px;
    margin-bottom: 8px;
    padding: 0 8px;
    overflow: hidden;
    text-overflow: ellipsis;
    -webkit-line-clamp: 2;
    /*display: -webkit-box;*/
    -webkit-box-orient: vertical;
    overflow-wrap: break-word;
  }
  .bottom {
    font-size: 12px;
    color: #9199A1;
    line-height: 18px;
    padding: 0 8px;
    margin-bottom: 18px;
    .price {
      line-height: 20px;
      margin-right: 2px;
      color: $--color-primary;
      float: left;
      font-weight: 700!important;
    }
    .learn {
      float: right;
    }
  }
  .clearfix:after {
    content: '\0020';
    display: block;
    height: 0;
    clear: both;
    visibility: hidden;
  }
}
</style>
